#include "LinkStack.h"

void initStack(LinkStack& S)
{
	S = NULL;
}

void clearStack(LinkStack& S)
{
	LinkNode* p;
	while (S)
	{
		p = S;
		S = S->link;
		free(p);
	}
}

bool stackEmpty(LinkStack& S)
{
	return S == NULL;
}

bool Push(LinkStack& S, SElemType x)
{
	LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
	p->data = x;
	p->link = S;
	S = p;
	return true;
}

bool Pop(LinkStack& S,SElemType& x)
{
	if (S == NULL)
	{
		return false;
	}
	LinkNode* p = S;
	x = S->data;
	S = S->link;
	free(p);
	return true;
}

bool getTop(LinkStack& S, SElemType& x)
{
	if (S == NULL)
	{
		return false;
	}
	x = S->data;
	return true;
}

int stackSize(LinkStack& S)
{
	LinkNode* p = S;
	int k = 0;
	while (p)
	{
		p = p->link;
		k++;
	}
	return k;
}